package com.config.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.config.dto.PageRequest;
import com.config.dto.PageResult;
import com.config.dto.Result;
import com.config.entity.SysUser;
import com.config.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户信息表(SysUser)表控制层
 *
 * @author alex
 * @since 2022-08-13 22:18:36
 */
@RestController
@RequestMapping("sysUser")
@Api(tags = "用户信息表")
public class SysUserController {

  @Autowired
  private SysUserService sysUserService;

  /**
   * 新增用户信息表
   *
   * @param sysUser
   * @return Result<Long>
   * @author alex
   * @date
   */
  @PostMapping
  @ApiOperation(value = "新增用户信息表")
  public Result<Long> add(@RequestBody SysUser sysUser) {
    sysUserService.save(sysUser);
    return Result.success();
  }

  /**
   * 删除用户信息表
   *
   * @param userId 主键
   * @return Result<Boolean>
   * @author alex
   * @date
   */
  @ApiOperation(value = "删除用户信息表")
  @DeleteMapping("/{userId}")
  public Result<Boolean> delete(@PathVariable(value = "userId") Long userId) {
    boolean flag = sysUserService.removeById(userId);
    return Result.success(flag);
  }

  /**
   * 修改用户信息表
   *
   * @param sysUser
   * @param userId
   * @return Result<Boolean>
   * @author alex
   * @date
   */
  @ApiOperation(value = "修改用户信息表")
  @PutMapping("/{userId}")
  public Result<Boolean> update(@PathVariable(value = "userId") Long userId, @RequestBody SysUser sysUser) {
    sysUser.setUserId(userId);
    boolean flag = sysUserService.updateById(sysUser);
    return Result.success(flag);
  }

  /**
   * 用户信息表详情
   *
   * @param userId
   * @return Result<SysUser>
   * @author alex
   * @date
   */
  @ApiOperation(value = "用户信息表详情")
  @GetMapping("/{userId}")
  public Result<SysUser> detail(@PathVariable(value = "userId") Long userId) {
    SysUser sysUser = sysUserService.getById(userId);
    return Result.success(sysUser);
  }

  /**
   * 分页查询用户信息表
   *
   * @return PageResult<SysUser>
   * @author alex
   * @date
   */
  @ApiOperation(value = "分页查询用户信息表")
  @GetMapping
  public PageResult<SysUser> list(@ApiParam(value = "分页查询参数") PageRequest pageRequest) {
    LambdaQueryWrapper<SysUser> lambdaQuery = Wrappers.lambdaQuery();
    Page<SysUser> page = sysUserService.page(pageRequest.of(), lambdaQuery);
    return PageResult.success(page);
  }

}


